<?php
class Teach {
	private $message = "";
	function Teach() {
		
	}

	public function run() {
		$_SESSION['tj']['is_first_enter'] = isset ($_SESSION['tj']['is_first_enter']) ? 1 : 0;
		if ($_SESSION['tj']['is_first_enter'] == 0) {
			$this->message = "进入调教模式了哦，今天要教点什么？ 呐~~  退出输入 -q";
			return;
		} 
		preg_match('/^-(\S+)\s*(.*)$/is', trim($_POST[Message]), $m);
		if (isset($_SESSION['tj']['id'])||$m) {
			
			$funName = "__" . $m[1];
			
				if(method_exists($this,$funName))
				{
					$this-> $funName($m);
				}else{
					$this->message = "还要做一些其他的修改吗= = \n -t\t设定回复的开始时间和结束时间范围";
				}
		} else {
			if(!isset($_SESSION['tj']['answer_flag'])) {
					  $_SESSION['tj']['answer_flag']=1;
						$this->message = "阿勒?当你说[{$_POST[Message]}]时，索妮娅酱该怎样回答呢？";
						$_SESSION['tj']['answer']=$_POST[Message];
						return;
	      	}else{
	      	
	      		
//	      	require_once (ROOT_PATH . "Lib/Mode/Study.class.php");
//			$study = new Study();
//			$study->getStudyAnswer();
//			$this->message = $study->message;
	      		
	      		
	      		$p='/^(是|说|回答|回复|答复)+?(.*)/is';
	      		preg_match($p, trim($_POST[Message]), $m);
	      	  if($m[2]){
	         	  $this->message = "好的，我记住了 ~\(≧▽≦)/~。";
	         	  $arr = array('question'=> trim($_SESSION['tj']['answer']),'qq'=> $_POST[Sender],'answer'=> trim($m[2]),'ClusterNum'=> $_POST[ClusterNum]);
	         	 $_SESSION['tj']['id'] = DB :: SaveTableRow("td_study", $arr);
	         	  echo  $_SESSION['tj']['id'];
	         	}else{
	         		$this->message = "没听明白- -~！";
	         	}
	      	}
		}
	}

	public function getTeachWord() {
		$filter_word_arr = require (ROOT_PATH . 'Common/keyword.php');
		foreach ($filter_word_arr as $v) {
			if (@ strpos("yaya" . $_POST[Message], $v)) {
				$_SESSION['DIAL_ERR_NUM'] = isset ($_SESSION['DIAL_ERR_NUM']) ? ($_SESSION['DIAL_ERR_NUM'] + 1) : 1;
				if ($_SESSION['DIAL_ERR_NUM'] == 1)
					$this->message = "咦？{$_POST[Nick]}！再对我说这些奇怪的话，我这几天就不理你啦>_<~!";
				else {
					$this->message = "哼！{$_POST[Nick]}！真的不理你啦>_<~!";
					$ret =  DB :: GetQueryResult("select * from td_blacklist where flag = 1 and  qq='{$_POST[Sender]}' and clusternum='0'", true);
					$start_time = date("YmdHis", (time() + 8 * 3600));
					$end_time = date("Ymd", (time() + (48+8) * 3600)) . "235959";
					if ($ret) {
						 $sql = "update  td_blacklist set start_time ='{$start_time}', end_time='{$end_time}',day=3,keword='{$v}' where flag=1 and qq='{$_POST[Sender]}' and clusternum='0'";
						
						 @ DB :: Query($sql);

					} else {
						$arr = array (
							'qq' => $_POST[Sender],
							'clusternum' => '0',
							'start_time' => $start_time,
							'end_time' => $end_time,
							'day' => 1,
							'keword' => $v
						);
						DB :: SaveTableRow('td_blacklist', $arr);
					}

					session_destroy();
				}
				return;
			}
		}

		$this->message = $this->getWord($_POST[Message]);

		if ($this->message == "") {
			$this->message = $this->getWordFromXiaoI($_POST[Message]);
		}

	}

	private function getWord($word) {
		$sql = "select * from td_study where  question ='" . trim($word) . "' and flag=1";

		//echo $sql;
		$ret = DB :: GetQueryResult($sql, false);
		$cou = count($ret);
		if ($cou != 0) {
			$my_arr = array ();
			$other_arr = array ();
			$i = 0;
			$j = 0;
			$k = 0;
			$z = 0;
			foreach ($ret as $key => $s) {
				if ($s[qq] == $_POST[Sender] && ($s[is_exec] == 0)) {
					$my_arr[$j++] = $s[answer];
					continue;
				}
				if ($s[qq] != $_POST[Sender] && ($s[is_exec] == 0)) {
					$other_arr[$z++] = $s[answer];
					continue;
				}
			}

			$arr = array ();
			if ($other_arr)
				$arr = $other_arr;
			if ($my_arr)
				$arr = $my_arr;

			$index = rand(0, count($arr) - 1);
			//print_r($ret);
			return $arr[$index];
		} else {
			return "";
		}

	}

	public function getWordFromXiaoI($word) {
		$url = "http://122.227.43.245/robot/demo/wap/wap-demo.action?requestContent=" . rawurlencode($word);
		$do = new CurlUtil($url);
		$do->init();
		$match_name = '/<p[^>]*class=\"wap_cn2\">.*?<\/span>(.*?)<\/p>/si';
		preg_match($match_name, $do->getBody(), $m);
		$i2 = preg_replace("/小i|xiao i|Xiao i|小I/", "索妮娅", $m[1]);
		$i3 = preg_replace("/你想问什么就问吧|甚麼？|学识浅薄的我实在听不懂你在说什么，默默的面壁去······|虽然索尼娅读不懂你的话，但索尼娅却能用心感受你对我的爱。|真心听不懂你在说什么，要么你换种问法试试如何？|是世界变化太快，还是我不够有才？为何你说话我不明白？|好吧，跟骨灰级大师聊天真心感觉压力好大呀，您老人家就不能来点简单点的么~|听的我一头雾水，阁下真是渊博呀，我需要膜拜~|看不懂，大侠，能不能换个说法？？？|讨厌~说的这么深奥，叫我肿么理解嘛！|恩？你到底在说什么呢？|唉，你输入的内容真的好深奥呀，能不能说点容易的，给人家留点面子嘛。|唧唧歪歪的说什么呢，实在是难懂。|没事耍什么深沉，害我都无法理解，来点简单的呗。|索尼娅现在理解能力有限，您那高级语言实在是让人捉摸不透呀。/", "", $i2);
		return strip_tags($i3);

	}
	
	private function __q($m) {
		if($_POST[Event]=="ReceiveNormalIM")
			unset ($_SESSION['NOWMODE']);
		else
			unset ($_SESSION['QUN_MODE']);
		unset ($_SESSION['tj']);
		$this->message = "退出调教模式了哦，多多和我说话吧，喵~！";
	}	
	
	private function __t($m) {
		
		
	}
	private function __get($property_name) {
		return $this-> $property_name;
	}

	private function __set($property_name, $value) {
		$this->$property_name = $value;
	}

}
?>